<?php
## include required files
/*******************************/
require_once '../../includefiles.php';
include_once("../../model/users.php");
require_once '../../model/trade.php';
require_once '../../model/admindetails.php';
include_once '../../model/email.php';
require_once '../../redirect.php';
## Create Objects
/*******************************/
$userObj = new Model_Users();
$tradeObj = new Model_TRADE();
$adminObj = new Model_AdminDetails();
$emailObj = new Model_Email();

/*******************************/

checkUserLogin();

if($_SESSION['user_type'] == '3')
{
	@header("location:".SITE_URL."/");
	exit;
}


$userId = $_SESSION['getbids_id'];
$userDetArray = $userObj->getUserDetailsByUserId($userId);
$smarty->assign('userDetArray',$userDetArray);

if($_GET['id2']!='')
{
	$pid= base64_decode($_GET['id2']); 
	$parentArray=$tradeObj->getTradeCategoryDetailsById($pid);
	$smarty->assign('parentArray', $parentArray);
	//echo '<pre>';print_r($moduelArray);exit;
}

if(isset($_POST['cat_name'])) 
{
	extract($_POST);
	
	//echo"<pre>";print_R($_POST);die();
	if(trim($_POST['cat_name']) != '') 
	{
		extract($_POST);
		$catArray['trade_category']   = return_post_value($cat_name);
		if($_GET['id2']!='')
		{
			$catArray['parent_id']   = base64_decode($_GET['id2']);
		}
		else
		{
			$catArray['parent_id']   = 0;
		}
		$catArray['status']   = 1;
		$catArray['user_id']   = $_SESSION['getbids_id'];
		
		if($_GET['id2']!='')
		{
			$getdetailtradetype = $tradeObj->checkTradeCatNameExits($_POST['cat_name']);
			if($getdetailtradetype['id']=='')
			{
				$getdetailjobtype = $tradeObj->checkJobTypeName($_POST['cat_name']);
				if($getdetailjobtype['id']=='')
				{
					$tradeObj->addTradeCategory($catArray);
					
					// send email message to Admin
					$email_message = $emailObj->getEmailById(17);
					$e_subject = $email_message['email_subject'];
					$message = $email_message['email_content']; 
					
					$subject = str_replace('[TYPE]', 'Job Type' , $e_subject);		 
					
					$message = str_replace('[TRADE_NAME]', trim($userDetArray["first_name"])." ".trim($userDetArray["last_name"]) , $message);
					$message = str_replace('[SITENAME]', SITENAME, $message);
					$message = str_replace('[TYPE]', 'Job Type', $message);
					$message = str_replace('[TYPE_NAME]', trim($parentArray['trade_category']), $message); 
					$message = str_replace('[JOBTYPE_NAME]', trim($catArray['trade_category']), $message); 
					
					
					$adminEmail = $adminObj->getAdminDetails();
					$to = $adminEmail;
					$from =$userDetArray["email"];
					$fromname= SITENAME;
									 
					$emailTemplate = file_get_contents('../../emailtemplate.html');
					$template_msg = str_replace('[SITENAME]',SITENAME , $emailTemplate);
					$template_msg = str_replace('[LOGO]','<img src="'.SITE_URL.'/uploads/logo/'.$imageDetails.'">', $template_msg);
					$template_msg = str_replace('[MESSAGE]',$message,$template_msg); 
					//echo $to."<br>". $template_msg; exit;
					if(trim($to) != '')
					@send_mail($to, $template_msg, $subject, $from, $fromname); 
					
					$_SESSION['msg'] = "<div class='success_msg'><span>Job Type added successfully</span></div>";
					redirect_to_link(SITE_URL.'/myaccount/'.$_GET['id1'].'/'.$_GET['id2'].'/mytradecategory/');
						exit;
				}
				else
				{
					$_SESSION['msg'] = "<div class='error_msg'><span>Job type name already used</span></div>";
					
				}

			}
			else
			{
				$_SESSION['msg'] = "<div class='error_msg'><span>Category name already used</span></div>";
				
			}		
		}
		else
		{
			$getdetailtradetype = $tradeObj->checkTradeCatNameExits($_POST['cat_name']);
			if($getdetailtradetype['id']=='')
			{
				$tradeObj->addTradeCategory($catArray);
				
				// send email message to Admin
				$email_message = $emailObj->getEmailById(17);
				$e_subject = $email_message['email_subject'];
				$message = $email_message['email_content']; 
				
				$subject = str_replace('[TYPE]', 'Trade Category' , $e_subject);		 
				
				$message = str_replace('[TRADE_NAME]', trim($userDetArray["first_name"])." ".trim($userDetArray["last_name"]) , $message);
				$message = str_replace('[SITENAME]', SITENAME, $message);
				$message = str_replace('[TYPE]', 'Trade Category', $message);
				$message = str_replace('[TYPE_NAME]', trim($catArray['trade_category']), $message); 
				$message = str_replace('[JOBTYPE_NAME]', "-", $message); 
				
				$adminEmail = $adminObj->getAdminDetails();
				$to = $adminEmail;
				$from =$userDetArray["email"];
				$fromname= SITENAME;
								 
				$emailTemplate = file_get_contents('../../emailtemplate.html');
				$template_msg = str_replace('[SITENAME]',SITENAME , $emailTemplate);
				$template_msg = str_replace('[LOGO]','<img src="'.SITE_URL.'/uploads/logo/'.$imageDetails.'">', $template_msg);
				$template_msg = str_replace('[MESSAGE]',$message,$template_msg); 
				//echo $to."<br>". $template_msg; exit;
				if(trim($to) != '')
				@send_mail($to, $template_msg, $subject, $from, $fromname); 
				
				
				$_SESSION['msg'] = "<div class='success_msg'><span>Trade category added successfully</span></div>";			
				redirect_to_link(SITE_URL.'/myaccount/mytradecategory/');
				exit;
			}
			else
			{
				$_SESSION['msg'] = "<div class='error_msg'><span>Trade category name already used</span></div>";
				
			}	
		}
	
	} 
	else
	{ // php validation else
		$_SESSION['msg'] = '<div class="error_msg"><span>Please enter trade category</span></div>';		
	}
	// main if closed					
}



if($_SESSION['msg'] != ''){
	$smarty->assign("msg", $_SESSION['msg']);	
	unset($_SESSION['msg']);
}
$active = "myaccount";
$smarty->assign("active", $active);
$subactive ="mytradecategory";
$smarty->assign("subactive", $subactive);
$smarty->assign('mainmenu', '3');
$smarty->display(TEMPLATEDIR . '/modules/myaccount/addtradecategory.tpl');
?>